package com.spelet.simulation;

import java.io.File;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Files.FileType;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.scenes.scene2d.ui.utils.ScissorStack;
import com.spelet.StaticFonts;

public class LoadMapFile {
	boolean available; // Fix code for if not available.
	public int deltaY;
	Sprite bgborder;
	public File currentFile;
	File currentDir;
	public Sprite border;
	public Sprite marked;
	public Sprite up;
	public Sprite open;
	int clickedAt;
	Rectangle bounds;
	Rectangle rb;
	public LoadMapFile() {
		bgborder = new Sprite(new Texture(Gdx.files.internal("data/LoadFileMap/border.png")));
		bgborder.setX(Gdx.graphics.getWidth()*0.88f/2-bgborder.getWidth()/2);
		bgborder.setY(Gdx.graphics.getHeight()/2-bgborder.getHeight()/2*0.47f);
		clickedAt = 0;
		bounds = new Rectangle(0,0,0,0);
		rb = new Rectangle(0,0,0,0);
		border = new Sprite(new Texture(Gdx.files.internal("data/loadfile/border.png")));
		marked = new Sprite(new Texture(Gdx.files.internal("data/loadfile/marked.png")));
		up = new Sprite(new Texture(Gdx.files.internal("data/loadfile/up.png")));
		open = new Sprite(new Texture(Gdx.files.internal("data/loadfile/open.png")));
		if (Gdx.files.isExternalStorageAvailable()) {
			if (Gdx.files.external("GreenHatMan").exists() && Gdx.files.external("GreenHatMan").isDirectory()) {
				available = true;
				currentDir = Gdx.files.external("GreenHatMan").file();
			} else {
				Gdx.files.external("GreenHatMan").file().mkdir();
				available = true;
				currentDir = Gdx.files.external("GreenHatMan").file();
			}
		} else {
			available = false;
		}
	}
	public void render(OrthographicCamera cam, SpriteBatch sb) {
		rb.set(bgborder.getBoundingRectangle());
		bgborder.draw(sb);
		ScissorStack.calculateScissors(cam, sb.getTransformMatrix(), rb, rb);
		ScissorStack.pushScissors(rb);
		int i = 1;
		for (File f : currentDir.listFiles()) {
			if (f.isDirectory() || f.getName().endsWith(".GHM")) {
				if (f.equals(currentFile)) {
					marked.setPosition(bgborder.getX()+20,(bgborder.getY()+bgborder.getHeight()-5)-i*64+deltaY);
					marked.draw(sb);
				}
				border.setPosition(bgborder.getX()+20,(bgborder.getY()+bgborder.getHeight()-5)-i*64+deltaY);
				border.draw(sb);
				StaticFonts.basicFont.draw(sb, f.getName(), bgborder.getX()+40, (bgborder.getY()+bgborder.getHeight()-5)-i*64+40+deltaY);
				i++;
			}
		}
		up.setPosition(bgborder.getX()+bgborder.getWidth()-up.getWidth()-5, bgborder.getY()+bgborder.getHeight()-64-5);
		open.setPosition(bgborder.getX()+bgborder.getWidth()-open.getWidth()-5,bgborder.getY()+5);
		if (currentDir.getParent() == null) {
			up.draw(sb,0.7f);
		} else {
			up.draw(sb);	
		}
		if (currentFile == null) {
			open.draw(sb,0.7f);
		} else {
			open.draw(sb);	
		}
		ScissorStack.popScissors();
		sb.flush();
	}
	public boolean hit(int x, int y) {
		return bgborder.getBoundingRectangle().contains(x,y);
	}
	public void pressed(int x, int y) {
		int i = 1;
		for (File f : currentDir.listFiles()) {
			if (f.isDirectory() || f.getName().endsWith(".GHM")) {
				bounds.set(bgborder.getX()+20,(bgborder.getY()+bgborder.getHeight()-5)-i*64+deltaY,256,64);
				if (bounds.contains(x,y)) {
					currentFile = f;
				}
				i++;
			}
		}
		if (up.getBoundingRectangle().contains(x,y) && currentDir.getParentFile() != null) {
			currentDir = currentDir.getParentFile();
			currentFile = null;
		}
	}
	public void changeDelta(int delta) {
		deltaY += delta;
		if (deltaY < 0)
			deltaY = 0;
		System.out.println("DELTA"+delta);
	}
	public boolean clickedOpen() {
		if (currentFile.isDirectory()) {
			currentDir = currentFile;
			currentFile = null;
			deltaY = 0;
			return false;
		} else {
			return true;
		}
	}
	public String getCurrentFileData() {
		FileHandle f = Gdx.files.getFileHandle(currentFile.getPath(), FileType.Internal);
		return f.readString();
	}
}
